define(['bootstrapTable','layer','formValidate'],function (bootstrapTable,layer,formValidate) {
    return{
        init:function () {
            showData();
            submitCallRecords()
            /*检索*/
            $("#search").click(function(){

                $('#callRecordsTable').bootstrapTable("refresh");

            })
        }
    };
    function showData(){
        $('#callRecordsTable').bootstrapTable({
            url: "/callRecordsController/pageSelect", // 获取表格数据的url
            method:'post',
            cache: false,       // 设置为 false 禁用 AJAX 数据缓存， 默认为true
            striped: true,      //表格显示条纹，默认为false
            pagination: true,   // 在表格底部显示分页组件，默认false
            contentType:'application/x-www-form-urlencoded',
            sidePagination: 'server', // 设置为服务器端分页，必要
            queryParamsType : "",
            queryParams:function(params){
                return tem={
                    rows:params.pageSize,
                    page:params.pageNumber,
                    myPhone:$("#searchPhone").val(),
                    type:$("#searchType").val(),

                }
            },
            clickToSelect:false,
            columns: [
                {
                    title: '序号',
                    align: 'center',
                    formatter:showNumber,
                    width:'1%'
                },{
                    field: 'ck',
                    checkbox: true,
                    align: 'center',
                    valign: 'middle',
                    width:'1%'
                },
                {
                    field: 'myPhone',
                    title: '手机号',
                    align: 'center',
                    valign: 'middle',
                    width:'16%'

                }, {
                    field: 'type',
                    title: '类型',
                    align: 'center',
                    valign: 'middle',
                    width:'10%',
                    formatter:function (value, row, index) {
                        if(value==1){
                            return '呼出'
                        }else if(value==2){
                            return '呼入'
                        }
                    }
                }, {
                    field: 'otherPhone',
                    title: "对方手机号",
                    align: 'center',
                    valign: 'middle',
                    width:'16%'
                }, {
                    field: 'createTime',
                    title: "时间",
                    align: 'center',
                    valign: 'middle',
                    width:'16%',
                    formatter:function (value, row, index) {
                        return value.split('.')[0];
                    }
                }, {
                    field: 'group',
                    title: "组别",
                    align: 'center',
                    valign: 'middle',
                    width: '22%'
                }
            ],
            onLoadSuccess: function(data){  //加载成功时执行

            },
            onLoadError: function(){  //加载失败时执行
                //console.info("加载数据失败");
            }
        });
    }
    /**
     *  添加
     */
    function submitCallRecords(){
        $('.addBtn').on('click',function(){
            layer.open({
                type:1,
                title:"新增通话记录",
                anim:1,
                closeBtn:1,
                area:['60%','60%'],
                content:$('#addCallRecoders'),
                success:function(layero,index){
                    $('.addBtn').blur();
                    formValidate.ResetForm($("#addForm"));
                    $("#addForm").find('input').val('');
                }
            });
        });
        //重置输入框
        $('.reset').on('click',function(){
            formValidate.ResetForm($("#addForm"));
        });
        //添加输入框验证
        formValidate.validate($("#addForm"),{
                myPhone: {
                    required: true,
                    minlength: 11
                },
                otherPhone: {
                    required: true,
                    minlength: 11
                }
            },{
                myPhone: {
                    required:"请输入电话号",
                    minlength:"电话号必须5个字符以上"
                },
                otherPhone: {
                    required:"请输入对方电话号",
                    minlength:"电话号必须5个字符以上"
                },
            },function () {
                $.ajax({// 添加
                    url:"/callRecordsController/add",
                    dataType:"json",
                    type:"post",
                    data:{
                        "myPhone":$("#myPhone").val(),
                        "type":$("#type").val(),
                        "otherPhone":$("#otherPhone").val(),
                    },
                    success:function(result){
                        layer.msg('保存成功',{icon:1});
                        layer.closeAll();
                        formValidate.ResetForm($("#addForm"));
                        $('#callRecordsTable').bootstrapTable("refresh");
                    }
                })
        });
    }
    function showNumber(value, row, index){
        var options = $('#callRecordsTable').bootstrapTable('getOptions');
        return options.pageSize*(options.pageNumber-1)+index + 1;
    }

})